﻿/***************************************************************************
 //* Foxit Software Inc.
 //* Copyright(C) 2005-2012, all rights reserved.
 //*
 //* The following code is copyrighted and contains proprietary information
 //* and trade secrets of Foxit Software Inc.                                                      *
 ***************************************************************************/
/**
 * PDF Annot 基类
 */
define(function (require, exports, module) {
    require('../DataLevel.js');
    require('../Math/Point.js');
    var PDFRect = require('../Math/Rect.js');

    /**
     * Enum for supported annotation type.
     * @enum
     * @private
     */
    WebPDF.PDFData.AnnotType = {
        MarkUp: "Markup",
        Link: "Link",
        Widget: "Widget",
        TypeWriter : "Typewriter",
        Text: "Text",
        Ink: "Ink"
    };

    var AnnotFlag = {
        ANNOTFLAG_INVISIBLE: 1,
        ANNOTFLAG_HIDDEN: 2,
        ANNOTFLAG_PRINT: 4,
        ANNOTFLAG_NOZOOM: 8,
        ANNOTFLAG_NOROTATE: 0x10,
        ANNOTFLAG_NOVIEW: 0x20,
        ANNOTFLAG_READONLY: 0x40,
        ANNOTFLAG_LOCKED: 0x80,
        ANNOTFLAG_TOGGLENOVIEW: 0x100
    };

    /**
     * Enum for PDF annotation flags.
     * @enum
     * @private
     */
    WebPDF.PDFData.AnnotFlag = AnnotFlag;

    /**
     * Border style for PDF annotations.
     * @enum
     * @private
     */
    WebPDF.PDFData.BorderStyle = {
        BBS_SOLID: 0,
        BBS_DASH: 1,
        BBS_BEVELED: 2,
        BBS_INSET: 3,
        BBS_UNDERLINE: 4
    };

    WebPDF.PDFData.AnnotDataJSONFormat = {
        type: "ty",
        flag: "f",
        rect: "rc",
        content: "c",
        name: "n",
        borderWidth: "bw",
        borderStyle: "bs",
        color: "cl",

        highlight: "hl",
        quadPoints: "Quad",
        destinationInfo: "ds",

        openState: "op",
        modifyDate: "md",

        state: "st",
        stateModel: "stm",

        opacity: "ca",
        creationDate: "cd",
        subject: "subj",
        title: "tit",
        popup: "pop",
        replyChain: "rpy",
        stateList: "stat",

        action: "act",
        group: "grp",

        subType: "subty",
        textAnnotName: "txtn",

        fontSize: "fs",
        fontColor: "fc",
        fontName: "ftn",
        lineHeight: "lnh",
        fontHeight: "fh",
        textAlign: "q",
        it: "it",

        inkList: "ikl",
        inkColor: "ikc",
        inkBorderWidth: "ikw",

        rotate: "r",
        
        pageIndex: "number",
        annotations: "annots"
    };

    var AnnotDataJSONFormat = WebPDF.PDFData.AnnotDataJSONFormat;

    /**
     * @param jsonData
     * @constructor
     * @private
     */
    WebPDF.PDFData.PDFAnnot = function (jsonData) {
        this.annotJSONData = jsonData;
        this.actionList = {};
        /** 用来标识Annot是否是开始编辑文档前已有的，如果是已有的Annot，在删除Annot的时候，只是设置隐藏标识
         * 这样，后台处理复杂度降低*/
        this.isSourceAnnot = true;
    };

    WebPDF.PDFData.PDFAnnot.prototype = {
        /**
         * Get type of annotation
         * @returns {WebPDF.PDFData.AnnotType}
         */
        getType: function () {
            return this.annotJSONData[AnnotDataJSONFormat.type];
        },
        /**
         * Get flags of annotation
         * @returns {WebPDF.PDFData.AnnotFlag}
         */
        getFlags: function () {
            return this.annotJSONData[AnnotDataJSONFormat.flag];
        },
        /**
         * Get rotate of annotation
         * @returns {Number}  Valid value are 0,1,2,3
         */
        getRotate: function () {
            if (typeof this.annotJSONData[AnnotDataJSONFormat.rotate] != "undefined"
                && this.annotJSONData[AnnotDataJSONFormat.rotate] != null)
                return this.annotJSONData[AnnotDataJSONFormat.rotate];
            return 0;
        },
        /**
         * Set flags of annotation
         * @param {WebPDF.PDFData.AnnotFlag} flags
         */
        setFlags: function (flags) {
            this.annotJSONData[AnnotDataJSONFormat.flag] = flags;
        },
        /**
         * Check whether current annotation is visible.
         * @returns {boolean} return true if current annotation is visible, otherwise return false.
         */
        isVisible: function () {
            var flags = this.getFlags();
            return !((flags & AnnotFlag.ANNOTFLAG_HIDDEN) ||
                (flags & AnnotFlag.ANNOTFLAG_NOVIEW));
        },
        /**
         * Check whether current annotation can be print.
         * @returns {boolean}
         */
        canBePrint: function () {
            var nFlags = this.getFlags();
            return ((nFlags & AnnotFlag.ANNOTFLAG_PRINT) != 0);
        },
        /**
         * Hide current annotation.
         */
        hide: function () {
            this.setFlags(this.getFlags() | AnnotFlag.ANNOTFLAG_HIDDEN);
        },
        /**
         * Get rect of current annotation.
         * @returns {WebPDF.PDFRect} rect of annotation.
         */
        getRect: function () {
            var rectJSONData = this.annotJSONData[AnnotDataJSONFormat.rect];
            var rect = new PDFRect(rectJSONData[0], rectJSONData[1],
                rectJSONData[2], rectJSONData[3]);
            return rect;
        },
        /**
         * Set rect of current annotation.
         * @param {WebPDF.PDFRect} rect new rect to set.
         */
        setRect: function (rect) {
            var rectJSONData = this.annotJSONData[AnnotDataJSONFormat.rect];
            rectJSONData[0] = rect.left;
            rectJSONData[1] = rect.top;
            rectJSONData[2] = rect.right;
            rectJSONData[3] = rect.bottom;
        },
        /**
         * Get contents of current annotation.
         * @returns {String}
         */
        getContents: function () {
            return this.annotJSONData[AnnotDataJSONFormat.content];
        },
        /**
         * Set content of current annotation.
         * @param {String} content new content to set.
         */
        setContents: function (content) {
            this.annotJSONData[AnnotDataJSONFormat.content] = content;
        },
        /**
         * Get name of current annotation.
         * @returns {String} name of current annotation.
         */
        getAnnotName: function () {
            return this.annotJSONData[AnnotDataJSONFormat.name];
        },
        /**
         * Set name of current annotation.
         * @param {String} name new name to set.
         */
        setAnnotName: function (name) {
            this.annotJSONData[AnnotDataJSONFormat.name] = name;
        },
        /**
         * Set border width of current annotation.
         * @param {Number} width width to set.
         */
        setBorderWidth: function (width) {
            this.annotJSONData[AnnotDataJSONFormat.borderWidth] = width;
        },
        /**
         * Get border width of current annotation.
         * @returns {Number}
         */
        getBorderWidth: function () {
            return this.annotJSONData[AnnotDataJSONFormat.borderWidth];
        },
        /**
         * Set border style of current annotation.
         * @param {WebPDF.PDFData.BorderStyle} style new style to set
         */
        setBorderStyle: function (style) {
            this.annotJSONData[AnnotDataJSONFormat.borderStyle] = style;
        },
        /**
         * Get border style of current annotation
         * @returns {WebPDF.PDFData.BorderStyle}
         */
        getBorderStyle: function () {
            return this.annotJSONData[AnnotDataJSONFormat.borderStyle];
        },
        /**
         * Set color of current annotation
         * @param {String} color new color to set, the format is like #ffffff
         */
        setColor: function (color) {
            this.annotJSONData[AnnotDataJSONFormat.color] = color;
        },
        /**
         * Get color of current annotation
         * @returns {String}  color of current annotation, the format is like #ffffff
         */
        getColor: function () {
            return this.annotJSONData[AnnotDataJSONFormat.color];
        },
        /**
         * Get Quad points of current annotation
         * @returns {JSON}
         */
        getQuadPoints: function () {
            return this.annotJSONData[AnnotDataJSONFormat.quadPoints];
        },
        /**
         * Set Quad points of current annotation.
         * @param {JSON} quadPoints
         */
        setQuadPoints: function (quadPoints) {
            this.annotJSONData[AnnotDataJSONFormat.quadPoints] = quadPoints;
        },
        /**
         * Get modification date of current annotation.
         * @returns {String}
         */
        getModifyDate: function () {
            return this.annotJSONData[AnnotDataJSONFormat.modifyDate];
        },
        /**
         * Set modify date of current annotation.
         * @param modifyDate
         */
        setModifyDate: function (modifyDate) {
            this.annotJSONData[AnnotDataJSONFormat.modifyDate] = modifyDate;
        },
        /**
         * Get creation date of current annotation.
         * @returns {String}
         */
        getCreationDate: function () {
            return this.annotJSONData[AnnotDataJSONFormat.creationDate];
        },
        /**
         * Set creation date of current annotation.
         * @param creationDate
         */
        setCreationDate: function (creationDate) {
            this.annotJSONData[AnnotDataJSONFormat.creationDate] = creationDate;
        },
        /**
         * Check whether current annotation is new added, return false if it is new added.
         * @returns {isSourceAnnot}
         */
        isSourceAnnot: function () {
            return this.isSourceAnnot;
        },
        /**
         *
         * @param {Boolean} isSourceAnnot
         */
        setSourceAnnot: function (isSourceAnnot) {
            this.isSourceAnnot = isSourceAnnot;
        },
        /**
         * Get annotation data in JSON format
         * @returns {*}
         */
        getAnnotJSONData: function () {
            return this.annotJSONData;
        }
    };
    return WebPDF.PDFData.PDFAnnot;
});